﻿#include <QtGui>
#include "reportform.h"
#include <QFileDialog>
#include <QFile>
#include <QString>
#include <string>
#include "dbutil.h"
using namespace std;
ReportForm::ReportForm(QObject *parent):QObject(parent){}

void ReportForm::setFormMode(int mode){
    if(mode==4) tablename="minute";
    else if(mode==3) tablename="hour";
    else if(mode==2) tablename="day";
    else if(mode==1) tablename="month";
    setMode(mode);
}

void ReportForm::setMonthTime(int year, int month){
    string m_year=to_string(year);
    string m_month=to_string(month);
    StartTime=m_year+"-"+m_month+"-1";
}

void ReportForm::setYearTime(int year){
    string m_year=to_string(year);
    StartTime=m_year+"-1";
}

void ReportForm::setDayTime(QString time){
    string m_time=time.toStdString();
    StartTime=m_time+" 1";
}

void ReportForm::setMinuteTime(QString day, int hour){
    string m_day=day.toStdString();
    string m_hour=to_string(hour);
    StartTime=m_day+" "+m_hour+":00";
}

void ReportForm::setRange(int range){
    timerange=range;
}

void ReportForm::setMode(int mode){
    m_mode=mode;
}

QVector<QString> ReportForm::getGasData(string dataname){
    QVector<QString> res;
    Dbutil db;
    QSqlDatabase data= db.connect();
    QSqlQuery query_id;
    QSqlQuery query_data;
    string std_time=StartTime;
    string sql_id;
    if(dataname=="time") sql_id="select id from co_"+tablename+" where time = '"+std_time+"'";
    else sql_id="select id from "+dataname+"_"+tablename+" where time = '"+std_time+"'";
    const char *k_id = sql_id.c_str();
    int id;
    bool success_id=query_id.exec(k_id);
    if(success_id) {
        while(query_id.next()) id=query_id.value(0).toInt();
    }else{
        qDebug()<<"数据不存在..";
        return res;
    }
    string sql_data;
    if(dataname=="time") sql_data="select time from co_"+tablename+" where id >="+to_string(id)+" and id <"+to_string(id+timerange);
    else sql_data="select concentration from "+dataname+"_"+tablename+" where id >="+to_string(id)+" and id <"+to_string(id+timerange);
    const char *k_data = sql_data.c_str();
    bool success_data = query_data.exec(k_data);
    if(success_data){
        while(query_data.next()){
            QString a=query_data.value(0).toString();
            //string t=a.toStdString();
            res.push_back(a);
        }
    }else qDebug()<<"数据不存在";
    db.dbclose(data);
    return res;
}

void ReportForm::report(){

    QVector<QString> particulates=getGasData("particulates");
    QVector<QString> so2=getGasData("so2");
    QVector<QString> nox=getGasData("nox");
    QVector<QString> co=getGasData("co");
    QVector<QString> timeset=getGasData("time");
    QVector<QString> o2=getGasData("o2");
    QString filepath="E:/123.csv";
    QString space("\n");
    QString type;
    if(m_mode==3) type="日";
    else if(m_mode==4) type="分钟";
    else if(m_mode==2) type="月";
    else if(m_mode==1) type="年";
    QString form_name(",,烟气"+type+"报表\n");
    QString source_name("排放源名称：,电厂,\n");
    QString source_number("排放源编号：,#1锅炉,\n");
    QString time(",,,,日期,2017 12 8,\n");
    QString gas_name("气体,CO,SO2,NOX,颗粒物,O2,\n");
    QString unit("时间,mg/Nm3,mg/Nm3,mg/Nm3,mg/Nm3,mg/Nm3,\n");
    QFile csvFile(filepath);
    if (csvFile.open(QIODevice::ReadWrite))
    {
        csvFile.write(space.toLocal8Bit());
        csvFile.write(space.toLocal8Bit());
        csvFile.write(form_name.toLocal8Bit());
        csvFile.write(space.toLocal8Bit());
        csvFile.write(source_name.toLocal8Bit());
        csvFile.write(source_number.toLocal8Bit());
        csvFile.write(time.toLocal8Bit());
        csvFile.write(gas_name.toLocal8Bit());
        csvFile.write(unit.toLocal8Bit());
        for(int gas=0;gas<co.size();gas++){
            QString data(timeset[gas]+","+co[gas]+","+so2[gas]+","+nox[gas]+","+particulates[gas]+","+o2[gas]+","+",\n");
            csvFile.write(data.toLocal8Bit());
        }
        //qDebug()<<csvFile.fileName();
        csvFile.close();
        csvFile.rename("E:/"+type+"报表.csv");
    }

}
